package com.estate.web.controller.report.report;

import com.estate.common.core.controller.BaseController;
import com.estate.common.core.domain.AjaxResult;
import com.estate.common.core.domain.entity.SysUser;
import com.estate.common.core.domain.model.LoginUser;
import com.estate.system.domain.vo.*;
import com.estate.system.service.impl.DataEnquiriesServiceImpl;
import com.estate.system.service.impl.DataListingsServiceImpl;
import com.estate.system.service.impl.TransactionDataServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/report/boss")
public class BossController extends BaseController {

    @Resource
    private DataEnquiriesServiceImpl dataEnquiriesServiceImpl;
    @Resource
    private DataListingsServiceImpl dataListingsService;
    @Resource
    private TransactionDataServiceImpl transactionDataService;

    /**
     * 获取近七天新增客户咨询数量
     *
     * @param paramData key1:xDays 表示要查询最近x天的数据
     * @return sevenDaysAgoCount：最近7天数量  fourteenDaysAgoCount：上一个7天的数量 addPercentage:最近7天新增百分比
     */
//    @PreAuthorize("@ss.hasPermi('sale:getSevenDayNewEnquire')")
    @GetMapping("/getKeyFindings")
    public AjaxResult getKeyFindings(Map<String, Object> paramData) {
        ZoneId australiaSydney = ZoneId.of("Australia/Sydney");
        ZonedDateTime now = ZonedDateTime.now(australiaSydney);
        // 设置日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String queryDate = now.format(formatter);
        String keyFinding = dataListingsService.getKeyFindings(queryDate,"boss_key_findings");
        List<String> findings =parseFindings(keyFinding);
        return AjaxResult.success("success", findings);
    }
    public static List<String> parseFindings(String text) {
        if (text == null || text.trim().isEmpty()) {
            return List.of();
        }

        // 按换行符分割
        String[] lines = text.split("\\r?\\n");

        return Arrays.stream(lines)
                .map(String::trim) // 去除前后空格
                .filter(line -> !line.isEmpty()) // 过滤空行
                .filter(line -> line.startsWith("-")) // 只保留Finding行
                .collect(Collectors.toList());
    }
    @GetMapping("/getMarketGrowthPotential")
    public AjaxResult getMarketGrowthPotential(Map<String, Object> paramData) {
        ZoneId australiaSydney = ZoneId.of("Australia/Sydney");
        ZonedDateTime now = ZonedDateTime.now(australiaSydney);
        // 设置日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String queryDate = now.format(formatter);
        String keyFinding = dataListingsService.getMarketGrowthPotential(queryDate);
        List<String> findings =parseFindings(keyFinding);
        return AjaxResult.success("success", findings);
    }

    @GetMapping("/getAreaAnalysis")
    public AjaxResult getDataSourceStatus(Map<String, Object> paramData) {
        Map<String, Object> result = new HashMap<>();
        //Popular Suburbs Distribution
        result.put("PopularSuburbsDistribution", dataEnquiriesServiceImpl.generateSuburbDistributionData(paramData));
        result.put("PropertyTypesByState", dataListingsService.generatePropertyTypeData(paramData));
        result.put("StateTransactionTypes", dataListingsService.generateTransactionTypeData(paramData));
        return AjaxResult.success("success", result);
    }

    @GetMapping("/getClientServiceAnalysis")
    public AjaxResult getClientServiceAnalysis(Map<String, Object> paramData) {
        Map<String, Object> result = new HashMap<>();
        result.put("ServiceTypeDistribution", dataEnquiriesServiceImpl.generateServiceTypeData(paramData));
        result.put("RecommendationStatusAnalysis", dataEnquiriesServiceImpl.generateRecommendationStatusData(paramData));
        result.put("ConnectionTypeVsRecommendationStatus", dataEnquiriesServiceImpl.generateConnectionRecommendationData(paramData));
        return AjaxResult.success("success", result);
    }
    @GetMapping("/getListingAnalysis")
    public AjaxResult getListingAnalysis(Map<String, Object> paramData) {
        Map<String, Object> result = new HashMap<>();
        result.put("PropertyTypeDistribution", dataListingsService.generatePropertyTypeData(paramData));
        result.put("TransactionTypeDistribution", transactionDataService.generateTransactionTypeData(paramData));
        result.put("PriceRangeDistribution", dataListingsService.generatePriceRangeData(paramData));
        result.put("TransactionTypeVsPropertyType", dataListingsService.generateHeatmapData(paramData));
        return AjaxResult.success("success", result);
    }

    @GetMapping("/getAgentPerformanceAnalysis")
    public AjaxResult getAgentPerformanceAnalysis(Map<String, Object> paramData) {
        Map<String, Object> result = new HashMap<>();
        result.put("TopAgentsByClientCount", dataEnquiriesServiceImpl.createTopAgentsByClientCount(paramData));
        result.put("AgentRecommendationRate", AgentPropertyAndTransactionRelation.createAgentRecommendationRate());
        result.put("AgentAndPropertyTypeRelationship", dataListingsService.createPropertyType(paramData));
        result.put("AgentAndTransactionTypeRelationship", AgentPropertyAndTransactionRelation.createTransactionType());
        result.put("AgentAndTransactionTypeRelationship", transactionDataService.createTransactionType(paramData));

        return AjaxResult.success("success", result);
    }
    @GetMapping("/getTimeTrends")
    public AjaxResult getTimeTrends(Map<String, Object> paramData) {
        Map<String, Object> result = new HashMap<>();
        result.put("MonthlyTrendPoint", dataEnquiriesServiceImpl.createMonthlyTrendPointData(paramData));
        result.put("DayOfWeekRequest", dataEnquiriesServiceImpl.createDayOfWeekRequestData(paramData));
        result.put("HourlyRequest", dataEnquiriesServiceImpl.createHourlyRequestData(paramData));

        return AjaxResult.success("success", result);
    }

}
